package com.edu.activiti.test;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;


/**
 * @author wu大人
 * @version 1.0
 * @date 2021/7/2 14:59
 */
public class ActivitiBusinessDemo {
    /**
     * TODO:添加BusinessKey到Activity的表中
     */
    @Test
    public void addBusinessKey() {
        //1.获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //3.启动流程的过程中，添加BusinessKey
        //第一个参数是流程定义的key：看bpmn文件就可以知道的
        //第二个参数是BusinessKey：自己看着业务来的
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myEvection", "1001");
        //4.输出
        System.out.println("BusinessKey=" + processInstance.getBusinessKey());
    }

    /**
     * TODO:全部流程实例的挂起激活测试(重点是我用的是流程定义来拿到所有流程实例)
     * suspend：暂停
     * 字段：SUSPENDSION_STATE:激活:1，挂起:2
     */
    @Test
    public void suspendAllProcessInstance() {
        //1.获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RepositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3.查询流程定义的信息
        ProcessDefinition processDefinition = repositoryService
                .createProcessDefinitionQuery()
                .processDefinitionKey("myEvection")
                .singleResult();
        //4.获取当前流程定义的实例是否都在挂起状态
        boolean suspended = processDefinition.isSuspended();
        //5.获取流程定义的ID
        String definitionId = processDefinition.getId();
        if (suspended) {
            //6.如果是挂起状态，则改成激活状态
            //参数1：流程定义ID；参数2：是否要激活；参数3：激活时间
            repositoryService.activateProcessDefinitionById(definitionId, true, null);
            System.out.println("流程定义ID：" + definitionId + "，已激活");
        } else {
            //7.如果是激活状态，则改为挂起状态
            //参数1：流程定义ID；参数2：是否要暂停；参数3：挂起时间
            repositoryService.suspendProcessDefinitionById(definitionId, true, null);
            System.out.println("流程定义ID：" + definitionId + "，已挂起");
        }
    }

    /**
     * TODO:单个流程实例的挂起或激活
     */
    @Test
    public void suspendSingleProcessInstance() {
        //1.获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RuntimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //3.根据RuntimeService获取流程实例对象
        ProcessInstance processInstance = runtimeService
                .createProcessInstanceQuery()
                .processInstanceId("7501")
                .singleResult();
        //4.得到当前流程实例对象的状态
        boolean suspended = processInstance.isSuspended();
        //5.获取流程实例ID
        String processInstanceId = processInstance.getId();
        if (suspended) {
            //6.判断是否已经暂停，如果已经暂停，就执行激活操作
            runtimeService.activateProcessInstanceById(processInstanceId);
            System.out.println("流程定义ID：" + processInstanceId + "，已激活");
        } else {
            //7.判断是否已经激活，如果已经激活，就执行挂起操作
            runtimeService.suspendProcessInstanceById(processInstanceId);
            System.out.println("流程定义ID：" + processInstanceId + "，已挂起");
        }
    }

    /**
     * TODO:完成个人任务
     * exception：Cannot complete a suspended task
     */
    @Test
    public void completTask(){
        //1.获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取TaskService
        TaskService taskService = processEngine.getTaskService();
        //3.使用TaskService获取任务
        Task task = taskService.createTaskQuery()
                .processInstanceId("7501")
                .taskAssignee("zhangsan")
                .singleResult();
        System.out.println("流程实例id="+task.getProcessInstanceId());
        System.out.println("流程任务 id="+task.getId());
        System.out.println("负责人="+task.getAssignee());
        System.out.println("任务名称="+task.getName());
        //4.根据任务id完成任务
        taskService.complete(task.getId());
    }
}
